/*
 * Background Flickr: Copyright (C) 2009  Craig McNicholas
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.ddw.app.bflickr.flickr.api.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ddw.app.bflickr.logging.Log;
import com.ddw.app.bflickr.logging.LogFactory;

/**
 * Class to handle website address values and validating them
 * @author craig
 *
 */
public class Website {

	private Log log = LogFactory.createInstance(this.getClass());
	private String website = null;
	
	/**
	 * Constructs a new website address instance
	 * @param website the website address to use
	 * @throws IllegalArgumentException when the website is not in a valid format, <i>website addresses cannot be invalid</i>
	 */
	public Website(String website) throws IllegalArgumentException {
		if(website != null) {
			if(website.trim().length() != 0 && validateURL(website)) {
				this.website = website;
			} else {
				log.error("The website address was not in the correct format.");
				throw new IllegalArgumentException("The website address was not in the correct format.");
			}
		} else {
			log.error("The website address cannot be null");
			throw new IllegalArgumentException("The website address cannot be null");
		}
	}
	
	/**
	 * Getter of the website address
	 * @return the website address string
	 */
	public String getWebsite() {
		return this.website;
	}
	
	@Override
	public String toString() {
		return this.website;
	}
	
	/**
	 * The expression to validate websites against. Making it final and static 
	 * provides performance benefits.
	 * 
	 * "^(https?://)"
			+ "(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //user@
			+ "(([0-9]{1,3}\\.){3}[0-9]{1,3}" // IP- 199.194.52.184
			+ "|" // allows either IP or domain
			+ "([0-9a-z_!~*'()-]+\\.)*" // tertiary domain(s)- www.
			+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." // second level domain
			+ "[a-z]{2,6})" // first level domain- .com or .museum
			+ "(:[0-9]{1,4})?" // port number- :80
			+ "((/?)|" // a slash isn't required if there is no file name
			+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"
	 */
	private static final String expression = "^http://(([0-9]{1,3}\\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\.[a-z]{2,6})(:[0-9]{1,4})?((/?)|(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
	
	/**
	 * The compiled patten to validate websites against. Making it final and static 
	 * provides performance benefits by allowing the validator to not have to compile costly 
	 * expressions over and over.
	 */
	private static final Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
	
	/**
	 * The pattern matcher used to validate websites against. Making it static 
	 * provides performance benefits by allowing the validator to reuse the matcher.
	 */
	private static Matcher matcher = null;
	
	/**
	 * Validates a url to make sure it is in the correct format using 
	 * a regular expression.
	 * @param url the url to validate
	 * @return a flag indicating whether the url is valid
	 */
	public static boolean validateURL(String url) {
		if(matcher == null) {
			matcher = pattern.matcher(url);
		} else { // Reuse matcher if instance is valid
			matcher.reset(url);
		}
		return matcher.matches();
	}
	
}
